//
//  ViewController.m
//  sd
//
//  Created by mac on 16-5-24.
//  Copyright (c) 2016年 zz. All rights reserved.
//
#import "ViewController.h"
#import "TWOViewController.h"
#import "Person.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    
    NSMutableArray*data;
}

@end

@implementation ViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor redColor];
    self.title=@"导航标题";
    self.refreshControl=[UIRefreshControl new];
    //页面刷新
    self.refreshControl.attributedTitle=[[NSAttributedString alloc]initWithString:@"看我刷新" attributes:@{NSForegroundColorAttributeName: [UIColor greenColor]}];
    //改变刷新页面颜色
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"5"]  style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];
    UIImageView *vi=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"4"]];
    self.navigationItem.titleView=vi;
    //使用图片来做按钮
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"按钮" style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];
    //刷新结束按钮
    UITableView *tableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain ];
    tableview.dataSource=self;
    tableview.delegate=self;
    [self.view addSubview:tableview];
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *url = [bundle URLForResource:@"00001" withExtension:@"vcf"];
    NSString *text = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取数据：%@", text);
    
    NSArray *arr = [text componentsSeparatedByString:@"\n"];
    Person*item;
    NSMutableArray *tmp = [NSMutableArray new];
    if (item!=NULL) {
        NSLog(@"sad");
    }
    for(NSString*p in arr){
        if([p hasPrefix:@"BEGIN"])
        {
            item=[Person new];
        }else if([p hasPrefix:@"END"])
        {
            [tmp addObject:item];
        }
//            else if([p hasPrefix:@"TEL"])
//        {
//            
//            NSArray *components=[p componentsSeparatedByString:@":"];
//            NSString*app=components[1];
//            item.stl=app;
//        }
        else if([p hasPrefix:@"FN:"]){
            NSArray *component=[p componentsSeparatedByString:@":"];
            NSString*abb=component[1];
            item.name=abb;
        }
        data=tmp;
    }
}
-(void)onClick:(id)sender
{
    [self.refreshControl endRefreshing];
}
//刷新结束按钮
//设置响应程序
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // return 10;
    return data.count;
    
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell*call=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    NSString *imgName=[NSString stringWithFormat:@"%ld",indexPath.row+1];
    //添加图片
    call.imageView.layer.cornerRadius=18;
   
    call.imageView.clipsToBounds = YES;
     //使图片变成圆的
    call.imageView.image=[UIImage imageNamed:imgName];
    Person *p = data[indexPath.row];
    call.textLabel.text = p.name;
    call.detailTextLabel.text = p.stl;
    call.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return call;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Person *d=data[indexPath.row];
    NSString*phone=[NSString stringWithFormat:@"tel://%@",d.stl];
    NSURL*url=[NSURL URLWithString:phone];
    UIApplication*zxc=[UIApplication sharedApplication];
    [zxc openURL:url];
    ViewController *con=[ViewController new];
    [self.navigationController pushViewController:con animated:YES];
    
    TWOViewController *cen=[TWOViewController new];
    [self.navigationController pushViewController:cen animated:YES];
    //改变二层次
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end